package com.ikun.hospayservice.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;
/**
 * 超时订单的配置
 */
@Slf4j
@Configuration
public class TimeoutOrderMQConfig {
    //正常交换机
    public static final String ORDER_EXCHANGE = "order-exchange";
    //正常队列
    public static final String ORDER_QUEUE = "order-queue";
    //死信交换机
    public static final String DEADLETTER_ORDER_EXCHANGE = "deadletter-order-exchange";
    //死信队列
    public static final String DEADLETTER_ORDER_QUEUE = "deadletter-order-queue";
    //正常路由键
    public static final String ORDER_QUEUE_KEY = "order-queue-routekey";
    //死信路由键
    public static final String DEADLETTER_ORDER_QUEUE_KEY = "deadletter-order-queue-routekey";

    /**
     * 正常交换机
     * @return
     */
    @Bean
    public TopicExchange topicExchange() {
        return new TopicExchange(ORDER_EXCHANGE);
    }

    /**
     * 绑定正常交换机和队列
     * @return
     */
    @Bean
    public Binding bindCourseSave() {
        return BindingBuilder.bind(queue()).to(topicExchange()).with(ORDER_QUEUE_KEY);
    }

    /**
     * 正常队列
     * @return
     */
    @Bean
    public Queue queue() {
        Map<String,Object> args = new HashMap<>();
        //设置死信交换机
        args.put("x-dead-letter-exchange",DEADLETTER_ORDER_EXCHANGE);
        //设置死信路由键
        args.put("x-dead-letter-routing-key",DEADLETTER_ORDER_QUEUE_KEY);
        return QueueBuilder.durable(ORDER_QUEUE).withArguments(args).build();
    }

    /**
     * 死信交换机
     * @return
     */
    @Bean
    public TopicExchange deadLetterExchange() {
        return new TopicExchange(DEADLETTER_ORDER_EXCHANGE);
    }

    /**
     * 绑定正常交换机和队列
     * @return
     */
    @Bean
    public Binding deadLetterQueueBinding() {
        return BindingBuilder.bind(deadLetterQueue()).to(deadLetterExchange()).with(DEADLETTER_ORDER_QUEUE_KEY);
    }

    /**
     * 死信队列
     * @return
     */
    @Bean
    public Queue deadLetterQueue() {
        return new Queue(DEADLETTER_ORDER_QUEUE);
    }
}
